package sg.edu.nus.iss.ooad.jass.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;

/**
 *
 * Advertiser value object. This class is value object representing database table
 * Advertiser. This class is intended to be used together with associated DAO object.
 * This class enscapsulates a particular Advertiser.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 * Development Environment : Netbeans IDE v6.7
 * Name of the File        : Advertiser.java
 * Creation / Modification History
 *         Isak Rabin        20-Sep-2009        Created
 *
 */
public class Advertiser implements IJASSEntity, Serializable {

        private static final long serialVersionUID = 6383276852317850004L;
        //Hold the attributes of Advertiser Entity
        public static final String[] ADVERTISER_ATTRIBUTES = {"ID", "UserID", "Name", "Password", "Deleted"};
        /**
         * Persistent Instance variables.
         * This data is directly  mapped to the columns of database table.
         */
        private String id = "";
        private String userID = "";
        private String name = "";
        private String password = "";
        private String deleted = "";
        /**
         * This data holds a reference to another entity (Associations).
         */
        private Collection<JobAdvertisement> jobadvertisement;

        /**
         * Default Contructor for Advertiser Class.
         */
        public Advertiser() {
                jobadvertisement = new ArrayList<JobAdvertisement>();
        }

        /**
         * Constructor of Advertiser class.
         * @param id  new value of ID
         * @param userId new value of userID
         * @param name new value of Name
         * @param password new value of Password
         * @param deleted new value of deleted
         */
        public Advertiser(String id, String userID, String name, String password, String deleted) {
                this();
                this.id = id;
                this.userID = userID;
                this.name = name;
                this.password = password;
                this.deleted = deleted;
        }

        /**
         * Get the value of Id
         * @return the value of Id
         */
        public String getID() {
                return id;
        }

        /**
         * Set the value of Id
         * @param id new value of Id
         */
        public void setID(String id) {
                this.id = id;
        }

        /**
         * Get the value of userID
         * @return the value of userID
         */
        public String getUserID() {
                return userID;
        }

        /**
         * Set the value of userID
         * @param userID new value of userID
         */
        public void setUserID(String userID) {
                this.userID = userID;
        }

        /**
         * Get the value of name
         * @return the value of name
         */
        public String getName() {
                return name;
        }

        /**
         * Set the value of name
         * @param name new value of name
         */
        public void setName(String name) {
                this.name = name;
        }

        /**
         * Get the value of password
         * @return the value of password
         */
        public String getPassword() {
                return password;
        }

        /**
         * Set the value of password
         * @param password new value of password
         */
        public void setPassword(String password) {
                this.password = password;
        }

        /**
         * Get the value of deleted
         * @return the value of deleted
         */
        public String getDeleted() {
                return deleted;
        }

        /**
         * Set the value of deleted
         * @param deleted Deleted flag for persistency
         */
        public void setDeleted(String deleted) {
                this.deleted = deleted;
        }

        /**
         * Get the value of jobadvertisement
         * @return the value of jobadvertisement
         */
        public Collection<JobAdvertisement> getJobAdvertisement() {
                return jobadvertisement;
        }

        /**
         * Set the value of jobadvertisement
         * @param jobadvertisement new value of jobadvertisement
         */
        public void setJobAdvertisement(Collection<JobAdvertisement> jobadvertisement) {
                this.jobadvertisement = jobadvertisement;
        }

        /**
         * Convert Properties into Advertiser entity.
         * @return Hashtable of Advertiser entity.
         */
        public Hashtable<String, IJASSEntity> fromJASSPropertiesFormat(Properties properties) {
                Hashtable<String, IJASSEntity> advertisers = new Hashtable<String, IJASSEntity>();

                String sTotalRecords = properties.getProperty("NumOfItems", "0");
                int iTotalRecords = Integer.parseInt(sTotalRecords);
                for (int i = 1; i <= iTotalRecords; i++) {
                        //ADVERTISER_ATTRIBUTES = {"ID", "UserID", "Name", "Password", "Deleted"};
                        String[] values = new String[ADVERTISER_ATTRIBUTES.length];
                        for (int j = 0; j < ADVERTISER_ATTRIBUTES.length; j++) {
                                values[j] = properties.getProperty(ADVERTISER_ATTRIBUTES[j] + String.valueOf(i));
                        }
                        Advertiser advertiser = new Advertiser(values[0], values[1], values[2], values[3], values[4]);
                        advertisers.put(values[0], advertiser);
                }

                return advertisers;
        }

        /**
         * Convert Advertiser entity into Properties format.
         * @param collections Collection of Advertiser entity.
         * @return The Properties of Advertiser.
         */
        public Properties toJassPropertiesFormat(Collection<IJASSEntity> collections) {
                Properties properties = new Properties();

                int count = 0;
                for (Iterator<IJASSEntity> i = collections.iterator(); i.hasNext();) {
                        count = count + 1;

                        //ADVERTISER_ATTRIBUTES = {"ID", "UserID", "Name", "Password", "Deleted"};
                        Advertiser advertiser = (Advertiser) i.next();
                        properties.setProperty(ADVERTISER_ATTRIBUTES[0] + String.valueOf(count), advertiser.getID());
                        properties.setProperty(ADVERTISER_ATTRIBUTES[1] + String.valueOf(count), advertiser.getUserID());
                        properties.setProperty(ADVERTISER_ATTRIBUTES[2] + String.valueOf(count), advertiser.getName());
                        properties.setProperty(ADVERTISER_ATTRIBUTES[3] + String.valueOf(count), advertiser.getPassword());
                        properties.setProperty(ADVERTISER_ATTRIBUTES[4] + String.valueOf(count), advertiser.getDeleted());
                }
                properties.setProperty("NumOfItems", String.valueOf(collections.size()));

                return properties;
        }

        @Override
        public String toString() {
                return super.toString();
        }
}
